import argparse
import os
import fnmatch
#from shutil import copytree

DIR_DICT = {'command-line': 'command-line', 'shared-lib': 'shared-lib', 'static-lib': 'static-lib'}
PATTERN_DICT = {'command-line': '@prog_name', 'shared-lib': '@class_name', 'static-lib': '@class_name'}

class Project():
	@staticmethod
	def create(args):
		proj_name = args.name[0]
		proj_type = args.type[0]
		proj_dir = os.path.join(os.curdir, proj_name)

		if not os.path.exists(proj_dir):
			#copy template project from the folder "template"
			cmdline_dir = os.path.join(os.curdir, "template", DIR_DICT[proj_type])
			os.mkdir(proj_dir)
			Project.mkfiles_from_template(cmdline_dir, proj_dir, PATTERN_DICT[proj_type], proj_name, "*.tpl")
			
			print "Project '%s' is created." % proj_name	
		else:		
			print "Project '%s' exists already." % proj_name	
	
	@staticmethod
	def mkfiles_from_template(template_dir, target_dir, find, replace, file_pattern):
		for path, dirs, files in os.walk(template_dir):
			for file_name in fnmatch.filter(files, file_pattern):
				file_path = os.path.join(path, file_name)
        			with open(file_path) as f:
            				s = f.read()
        			s = s.replace(find, replace)
				base_name = os.path.basename(file_path)
				var_target_dir = target_dir
				parent_dir_name = os.path.dirname(file_path)

				if(parent_dir_name != template_dir):
					var_target_dir = os.path.join(var_target_dir, os.path.basename(parent_dir_name))
					var_target_dir = var_target_dir.replace(find, replace).replace('.tpl', '')
					if not os.path.exists(var_target_dir):
						os.mkdir(var_target_dir.replace(find, replace).replace('.tpl', ''))

				new_path = os.path.join(var_target_dir, base_name.replace(find, replace).replace('.tpl', ''))
        			with open(new_path, "w") as f:
            				f.write(s)
	@staticmethod
	def remove(args):
		proj_name = args.name[0]
		proj_dir = os.path.join(os.curdir, proj_name)

		if os.path.exists(proj_dir):
			os.rmdir(proj_dir)	
			print "Project '%s' is removed." % proj_name	
		else:		
			print "Project '%s' dose not exist." % proj_name	
		
def main():
        # main parser
	desc = 'The scons-admin script uses SCons to manage C++ projects.'
	epil = 'It enables maven-stlye project management for C++.'
	parser = argparse.ArgumentParser(description=desc, epilog=epil)
	parser.add_argument('-v','--version', action='version', version='0.1')
	
	# sub parsers
	sub_parsers = parser.add_subparsers()
        ## parser for creating project
	parser_create = sub_parsers.add_parser('createproject')
	parser_create.add_argument('name', nargs=1, help='create a new project')
	parser_create.add_argument('-t', '--type', nargs=1, help='set the project type')
	parser_create.set_defaults(func=Project.create)

        ## parser for creating project
	parser_remove = sub_parsers.add_parser('removeproject')
	parser_remove.add_argument('name', nargs=1, help='remove an existing project')
	parser_remove.set_defaults(func=Project.remove)
	
	# dispatch args to different registed functions
	args = parser.parse_args()
	args.func(args)

if __name__ == "__main__":
	main()
